<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class MemberStatusInsureBase extends BaseItemBase {
	var $_id = 0;
	var $_item = array();
	
	//constructor
	function MemberStatusInsureBase() {
		parent::BaseItemBase();
		
		$this->_table_owner_field = 'uid';
		$this->table = 'pp_m_students_status_insure';
		$this->prefix = 's_';
		
		$this->sort_fields = array('id-');
	}
	
	//current
	function bind($owner = 0) {
		if( $owner == 0 ) {
			$this->owner = $this->_get_current_user();
		} else {
			$this->owner = $owner;
		}
		$item = $this->getItemBy($this->owner, $this->_table_owner_field);
		if( $item['id'] > 0 ) {
			$this->_id = $item['id'];
			$this->_item = $this->formatItem($item);
		}
	}
	
	function checkExist() {
		return (($this->_id > 0) ? true : false);
	}
	
	function get_ID() {
		return $this->_id;
	}
	
	function get_Item() {
		return $this->_item;
	}
	
	//methods
	function onAddItemBefore($data_array) {
		return false;
	}
	
	function onAddItemFilterBind($data_array) {
		$fb = array();
		$pks = array();
		$pms = array();
		$its = array();
		foreach($data_array as $k => $v) {
			if( substr($k, 0, strlen('s_package_')) == 's_package_' && substr($k, strlen('s_package_')) > 0 ) {
				$pks[] = substr($k, strlen('s_package_'));
			}
			if( substr($k, 0, strlen('s_payment_')) == 's_payment_' && substr($k, strlen('s_payment_')) > 0 ) {
				$pms[] = substr($k, strlen('s_payment_'));
			}
			if( substr($k, 0, strlen('s_items_')) == 's_items_' && substr($k, strlen('s_items_')) > 0 ) {
				$its[] = substr($k, strlen('s_items_'));
			}
		}
		$fb['s_packages'] = implode('|', $pks);
		$fb['s_payments'] = implode('|', $pms);
		$fb['s_items'] = implode('|', $its);
		
		return $fb;
	}
	
	function onUpdateItemBefore($items_id, $data_array) {
		return false;
	}
	
	function onUpdateItemFilterBind($items_id, $data_array) {
		$fb = array();
		$pks = array();
		$pms = array();
		$its = array();
		foreach($data_array as $k => $v) {
			if( substr($k, 0, strlen('s_package_')) == 's_package_' && substr($k, strlen('s_package_')) > 0 ) {
				$pks[] = substr($k, strlen('s_package_'));
			}
			if( substr($k, 0, strlen('s_payment_')) == 's_payment_' && substr($k, strlen('s_payment_')) > 0 ) {
				$pms[] = substr($k, strlen('s_payment_'));
			}
			if( substr($k, 0, strlen('s_items_')) == 's_items_' && substr($k, strlen('s_items_')) > 0 ) {
				$its[] = substr($k, strlen('s_items_'));
			}
		}
		$fb['s_packages'] = implode('|', $pks);
		$fb['s_payments'] = implode('|', $pms);
		$fb['s_items'] = implode('|', $its);
		
		return $fb;
	}
	
	//format
	function formatItem($item) {
		if( $item['packages'] != '' ) {
			$as = explode('|', $item['packages']);
			for($i=0, $n=sizeof($as); $i<$n; $i++) {
				$item['package_' . $as[$i]] = $as[$i];
			}
			$as = explode('|', $item['payments']);
			for($i=0, $n=sizeof($as); $i<$n; $i++) {
				$item['payment_' . $as[$i]] = $as[$i];
			}
			$as = explode('|', $item['items']);
			for($i=0, $n=sizeof($as); $i<$n; $i++) {
				$item['items_' . $as[$i]] = $as[$i];
			}
		}
		
		return $item;
	}
}
//
?>